/*
 * @copyright 2011 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of GameTimer.
 *
 * GameTimer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GameTimer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GameTimer. If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiption.gametimer.util;

import org.amphiption.gametimer.ApplicationConstants;
import org.amphiption.gametimer.entity.Player;
import org.amphiption.gametimer.entity.TimerConfiguration;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "amphiprion_gametimer";
	private static final int DATABASE_VERSION = 6;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL("create table TIMER_CONFIGURATION (" + TimerConfiguration.DbField.ID + " text primary key, " + TimerConfiguration.DbField.NAME + " text, "
					+ TimerConfiguration.DbField.TIMER_TYPE + " integer not null" + "," + TimerConfiguration.DbField.DURATION + " integer not null) ");

			db.execSQL("create table PLAYER (" + Player.DbField.ID + " text primary key, " + Player.DbField.NAME + " text," + Player.DbField.COLOR + " integer not null, "
					+ Player.DbField.FK_CONFIGURATION + " text not null)");

			onUpgrade(db, 1, DATABASE_VERSION);
		} catch (Throwable e) {
			Log.e(ApplicationConstants.PACKAGE, "", e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion == 1) {
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.HANDICAP + " integer default 0");
			oldVersion++;
		}
		if (oldVersion == 2) {
			db.execSQL("ALTER TABLE PLAYER ADD " + Player.DbField.DURATION + " integer default 0");
			oldVersion++;
		}
		if (oldVersion == 3) {
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.OWN_TUNE + " text");
			oldVersion++;
		}
		if (oldVersion == 4) {
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.BEFORE_WARN + " integer default 0");
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.EVERY_WARN + " integer default 1");
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.OWN_TUNE_WARN + " text");
			oldVersion++;
		}
		if (oldVersion == 5) {
			db.execSQL("ALTER TABLE TIMER_CONFIGURATION ADD " + TimerConfiguration.DbField.LIMIT_REPORT + " integer default 0");
			oldVersion++;
		}
	}

}
